package utils.db.impl;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import pojo.EUserType;
import pojo.User;
import utils.db.api.IUserAdd;

public class UserAdd extends DBOperation implements IUserAdd {
	private static Logger logger = LoggerFactory.getLogger(UserAdd.class);

	public UserAdd() {
		super();
	}
	@Override
	public void adduser(String firstName, String lastName, String password,
			String email, EUserType type, int phoneNumber) {
		try {
			initializeUserStatement(firstName, lastName, password, email, type,
					phoneNumber);			
		} catch (SQLException e) {
			logger.error("Error during create new user");
		}
		logger.info("Added: " + firstName + " " + lastName + " " + password
				+ " " + email + " " + type.name() + " " + phoneNumber);
	}
	@Override
	public void addUser(User u) {
		try {
			initializeUserStatement(u.getFirstName(), u.getLastName(), u.getPassword(), u.getEmail(), u.getType(),
					u.getPhoneNumber());			
		} catch (SQLException e) {
			logger.error("Error during create new user");
		}
		logger.info("Added: " + u.getFirstName() + " " + u.getLastName() + " " + u.getPassword()
				+ " " + u.getEmail() + " " + u.getType().name() + " " + u.getPhoneNumber());
	}

	private void initializeUserStatement(String firstName, String lastName,
			String password, String email, EUserType type, int phoneNumber)
			throws SQLException {
		pstmt = super.getConnector().prepareStatement(ADD_USER);
		pstmt.setString(1, firstName);
		pstmt.setString(2, lastName);
		pstmt.setString(3, password);
		pstmt.setString(4, email);
		pstmt.setString(5, type.name());
		pstmt.setInt(6, phoneNumber);
		pstmt.execute();
		pstmt.close();
	}

	

	public static void main(String[] args) {
		//UserAdd adder = new UserAdd();
//		adder.adduser("Zbygniew", "Boniek", "12345", "test@op.pl",
//				EUserType.ADMIN, 123456);
		//User u  = new User("Ricardo","Kaka","test","test@op.pl",EUserType.SERVICEMAN,12345);
		//adder.addUser(u);
		
	}
}
